package com.ruoyi.project.business.domain.rules;

import com.ruoyi.project.business.domain.entity.Emp;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.model.month.ShiftDefine;
import com.ruoyi.project.business.domain.utils.RuleUtil;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用 RuleUtil.initializeBean 实例化
 */
@Data
public abstract class Rule {
    /**
     * 1 ：用户规则  2：管理规则
     */
    protected Integer type;
    /**
     * 规则优先级
     */
    protected Long userId;
    protected Integer order;
    /**
     * 目标日期
     */
    protected ShiftDefine shift;
    /**
     * 如果不满足，则 break 整个活动的影响
     */
    protected Boolean breakIfReject;
    /**
     * break 的时候的提示
     */
    protected String breakTips;
    // 会冲突的规则
    protected List<Class> conflicts;

    /**
     * 接受
     */
    public static final int RESULT_ACCEPT = 1;
    /**
     * 拒绝当前规则链，中断当前的单个任务
     */
    public static final int REJECT_AND_CONTINUE_TASK = 2;
    /**
     * 终止整个任务
     */
    public static final int STOP_TASK = 3;

    public List<Rule> tryConflictWith(List<Rule> rules) {
        if(CollectionUtils.isEmpty(rules)) {
            return new ArrayList<>();
        }
        rules.add(this);
        List<Rule>  abandonedRules = RuleUtil.getAbandonedRules(rules);
        return abandonedRules;
    }

    public static JudgeResult accept() {
        JudgeResult res = new JudgeResult();
        res.setResult(RESULT_ACCEPT);
        return res;
    }

    public static JudgeResult acceptAndReturn() {
        JudgeResult res = new JudgeResult();
        res.setResult(REJECT_AND_CONTINUE_TASK);
        return res;
    }

    public static JudgeResult stopTask() {
        JudgeResult res = new JudgeResult();
        res.setResult(STOP_TASK);
        return res;
    }

    public static JudgeResult accept(String tips) {
        JudgeResult res = new JudgeResult();
        res.setResult(RESULT_ACCEPT);
        res.setTips(tips);
        return res;
    }

    public abstract JudgeResult judge(Solution solution, Emp emp);

    public abstract void setConflicts();

    /**
     * 1 ：用户规则  2：管理规则
     */
    public final static Integer RULE_TYPE_USER = 1;
    public final static Integer RULE_TYPE_MANAGER = 2;
}
